<template>
  <div class="search-bar">
    <div class="search-bar-wrapper">
      <van-icon
        name="search"
        class="search"
        size="16px"
        color="#858C96"
      />
      <input
        class="search-bar-input"
        :focus="focus"
        :disabled="disabled"
        :maxlength="limit"
        :placeholder="hotSearch.length === 0 ? '搜索' : hotSearch"
        v-model="searchWord"
        @input="onChange"
        comfirm-type="search"
        @confirm="onConfirm"
        placeholder-style="color: #ADB4BE"
      />
      <van-icon
        name="clear"
        class="clear"
        size="16px"
        color="#858C96"
        @click="onClearClick"
        v-if="searchWord.length > 0"
      />
    </div>
  </div>
</template>

<script>
export default {
  props: {
    focus: {
      type: Boolean,
      default: true
    },
    disabled: {
      type: Boolean,
      default: false
    },
    limit: {
      type: Number,
      default: 50
    },
    hotSearch: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      searchWord: ''
    }
  },
  methods: {
    onSearchBarClick () {
      this.$emit('onClick')
    },
    onClearClick () {
      this.searchWord = ''
      this.$emit('onClear')
    },
    onChange (e) {
      const { value } = e.mp.detail
      this.$emit('onChange', value)
    },
    onConfirm (e) {
      const { value } = e.mp.detail
      this.$emit('onConfirm', value)
    },
    setValue (value) {
      this.searchWord = value
    },
    getValue () {
      return this.searchWord
    }
  }
}
</script>

<style scoped lang="scss">
.search-bar {
  padding: 15px 15.5px;
  .search-bar-wrapper {
    background: #F5F7F9;
    border-radius: 20px;
    display: flex;
    align-items: center;
    padding: 12px 17px;
    height: 40px;
    box-sizing: border-box;
    .search-bar-input {
      flex: 1;
      margin: 0 8px;
    }
    .search, .clear {
      display: flex;
      height: 100%;
      align-items: center;
    }
  }
}
</style>
